﻿#New marriage interaction effect
arrange_marriage_interaction = {
	icon = icon_marriage
	category = interaction_category_diplomacy
	common_interaction = yes

	override_background = { reference = relaxing_room }

	special_interaction = arrange_marriage_interaction
	interface = marriage
	use_diplomatic_range = {
		trigger_if = {
			limit = { exists = scope:secondary_actor.betrothed }
			NOT = {
				scope:secondary_actor = {
					betrothed = scope:secondary_recipient
				}
			}
		}
	} 

	recipient_recieve_cooldown = {
		years = marriage_recieve_proposal_cooldown
	}

	ignore_recipient_recieve_cooldown = {
		AND = {
			is_ai = no
			scope:recipient = { is_ai = no }
		}
	}

	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:actor = scope:recipient
				}
				desc = arrange_marriage_interaction_desc_ruler_at_own_court
			}
			triggered_desc = {
				trigger = {
					scope:actor = {
						can_marry_character_trigger = {
							CHARACTER = scope:recipient
						}
					}
					scope:recipient = { is_ruler = yes }
				}
				desc = arrange_marriage_interaction_desc_both_rulers_can_marry
			}
			triggered_desc = {
				trigger = {
					scope:recipient = {
						is_ruler = yes
						can_marry_trigger = yes
					}
				}
				desc = arrange_marriage_interaction_desc_both_rulers_recipient_only_can_marry
			}
			triggered_desc = {
				trigger = {
					scope:recipient = {
						is_ruler = yes
					}
				}
				desc = arrange_marriage_interaction_desc_both_rulers_recipient_neither_can_marry
			}
			triggered_desc = {
				trigger = {
					scope:actor = {
						can_marry_character_trigger = {
							CHARACTER = scope:recipient
						}
					}
				}
				desc = arrange_marriage_interaction_desc_one_ruler_can_marry
			}
			desc = arrange_marriage_interaction_desc_recipient_one_ruler_cannot_marry
		}
	}
	
	redirect = {
		# If the define MARRIAGE_OWNER_IS_MATCHMAKER is set to yes, the actor must end up as the "matchmaker" link for courtiers
		scope:actor = {
			if = {
				limit = {
					exists = matchmaker
					NOT = { this = matchmaker }
				}
				save_scope_as = secondary_actor
				matchmaker = {
					save_scope_as = actor
				}
			}
			if = {
				limit = {
					is_betrothed = yes
					betrothed = scope:recipient
				}

				save_scope_as = secondary_actor
			}
		}

		scope:recipient = {
			if = {
				limit = {
					is_betrothed = yes
					betrothed.matchmaker = scope:actor
					is_adult = yes
					betrothed = {
						is_adult = yes
					}
				}

				save_scope_as = secondary_recipient
				matchmaker = {
					save_scope_as = recipient
				}
				betrothed = {
					save_scope_as = secondary_actor
				}
			}
			if = {
				limit = {
					exists = matchmaker
					NOT = { this = matchmaker }
				}
				save_scope_as = secondary_recipient
				matchmaker = {
					save_scope_as = recipient
				}
			}
		}
	}

	populate_actor_list = {
		scope:actor = {
			add_to_list = characters
			every_courtier = {
				limit = {
					trigger_if = {
						limit = {
							is_concubine = yes
						}
						is_concubine_of = scope:actor
						can_marry_character_trigger = { CHARACTER = scope:actor }
					}
				}
				add_to_list = characters
			}
			every_courtier_away = {
				limit = {
					trigger_if = {
						limit = {
							is_concubine = yes
						}
						is_concubine_of = scope:actor
						can_marry_character_trigger = { CHARACTER = scope:actor }
					}
				}
				add_to_list = characters
			}
			every_child = {
				even_if_dead = yes
				if = {
					limit = {
						is_alive = yes
						NOT = { is_in_list = characters }
						trigger_if = {
							limit = {
								is_concubine = yes
							}
							is_concubine_of = scope:actor
							can_marry_character_trigger = { CHARACTER = scope:actor }
						}
					}
					add_to_list = characters
				}
				every_child = {
					even_if_dead = yes
					if = {
						limit = {
							is_alive = yes
							NOT = { is_in_list = characters }
							OR = {
								top_liege = scope:actor
								house.house_head = scope:actor
								dynasty.dynast = scope:actor
							}
							trigger_if = {
								limit = {
									is_concubine = yes
								}
								is_concubine_of = scope:actor
								can_marry_character_trigger = { CHARACTER = scope:actor }
							}
						}
						add_to_list = characters
					}
					every_child = {
						if = {
							limit = {
								is_alive = yes
								NOT = { is_in_list = characters }
								OR = {
									top_liege = scope:actor
									house.house_head = scope:actor
									dynasty.dynast = scope:actor
								}
								trigger_if = {
									limit = {
										is_concubine = yes
									}
									is_concubine_of = scope:actor
									can_marry_character_trigger = { CHARACTER = scope:actor }
								}
							}
							add_to_list = characters
						}
					}
				}
			}
		}
	}

	populate_recipient_list = {
		scope:recipient = {
			add_to_list = characters
			every_courtier = {
				add_to_list = characters
			}
			every_courtier_away = {
				add_to_list = characters
			}
			every_child = {
				limit = {
					NOT = { is_in_list = characters }
				}
				add_to_list = characters
			}
		}
	}

	interface_priority = 60

	cost = {
		prestige = {
			value = 0
			if = {
				limit = {
					exists = scope:actor
					exists = scope:recipient
					exists = scope:secondary_recipient
					exists = scope:secondary_actor
					scope:secondary_actor = { is_close_family_of = scope:actor }
					scope:recipient = {
						OR = {
							AND = {
								vassal_contract_has_flag = vassal_contract_marriage_favor
								is_allied_to = scope:actor
							}
							NOT = { vassal_contract_has_flag = vassal_contract_marriage_favor}
						}
					}
				}
				add = {
					scope:actor = {
						every_vassal = {
							limit = {
								vassal_contract_has_flag = vassal_contract_marriage_favor
								NOT = { is_allied_to = scope:actor }
								NOR = {
									this = scope:recipient
									this = scope:secondary_recipient
								}
							}
							add = medium_prestige_value
						}
					}
					desc = VASSAL_WITH_MARRIAGE_FAVOR
				}
			}
		}

		piety = {
			value = 0
			if = {
				limit = { scope:piety_cost_reduction = yes }
				add = scope:actor.medium_piety_value
				desc = STRUGGLE_MARRIAGE_ACCEPTANCE_USING_PIETY_AS_CURRENCY
			}
		}

		influence = {
			value = 0
			if = {
				limit = { scope:influence_send_option = yes }
				add = scope:actor.medium_influence_value
				desc = INFLUENCE_INTERACTION_ACCEPTANCE_SEND_OPTION
			}
		}
	}

	# actor, recipient ... characters arranging the marriage
	# secondary_actor, secondary_recipient ... characters that should marry

	is_shown = {
		scope:actor = {
			OR = {
				is_ruler = yes #Can arrange marriages for others
				#You're just a courtier? Only use the interaction on characters you can marry
				can_marry_character_trigger = { CHARACTER = scope:recipient }
			}
		}
		# Block further marriage proposals for people who have a pending grand wedding
		NOR = {
			scope:secondary_actor ?= { has_been_promised_grand_wedding = yes }
			scope:secondary_recipient ?= { has_been_promised_grand_wedding = yes }
		}
		#If you're interacting with someone who isn't their own matchmaker, only show the interaction if they can get married. (If someone isn't their own matchmaker, they will be set as secondary_recipient by code and their matchmaker becomes recipient)
		trigger_if = {
			limit = { exists = scope:secondary_recipient }
			scope:secondary_recipient = {
				OR = {
					marriage_interaction_can_be_picked_trigger = yes #Can marry anyone
					AND = {
						ready_to_marry_betrothed_trigger = yes
						OR = {
							betrothed = scope:secondary_actor
							trigger_if = {
								limit = { exists = betrothed.liege }
								betrothed.liege = scope:actor
							}
						}
					}
				}
				#To account for a person marrying their concubines
				trigger_if = {
					limit = {
						is_concubine = yes
					}
					is_concubine_of = scope:actor
					can_marry_character_trigger = { CHARACTER = scope:actor }
				}
			}
		}
		#allow landless adventurers to marry only within their camp, or with Contact list courtiers (after using request_marriage option in contact_list_request_interaction), or prieviously betrothed characters
		trigger_if = {
			limit = {
				scope:actor = {
					is_landless_adventurer = yes
				}
			}
			trigger_if = {
				limit = {
					OR = {
						scope:actor = {
							is_target_in_variable_list = {
								name = contact_list_marriage
								target = scope:recipient
							}
						}
						scope:recipient = {
							is_betrothed = yes
							betrothed = {
								OR = {
									scope:actor = this
									scope:secondary_actor ?= this
								}
							}
						}
						scope:secondary_recipient ?= {
							is_betrothed = yes
							betrothed = {
								OR = {
									scope:actor = this
									scope:secondary_actor ?= this
								}
							}
						}
					}
				}
				always = yes
			}
			trigger_else = {
				scope:recipient = scope:actor
			}
		}
	}
	
	is_valid_showing_failures_only = {
		#If we're opening this interaction through the interaction menu (i.e. not all scopes exist yet)
		#This was giving double-warnings about a marry-able character existing so it's commented out for now
		#custom_description = {
		#	text = character_to_marry_exists
		#	object = scope:recipient
		#	scope:recipient = {
		#		OR = {
		#			any_courtier = {
		#				is_at_home = yes
		#				marriage_interaction_can_be_picked_trigger= yes
		#			}
		#			any_child = {
		#				marriage_interaction_can_be_picked_trigger= yes
		#			}
		#		}
		#	}
		#}
		trigger_if = {
			limit = {
				NAND = {
					exists = scope:secondary_actor
					exists = scope:secondary_recipient
				}
			}
			#There is an available marriage
			custom_description = {
				text = "basic_can_marry_check_recipient"
				subject = scope:actor
				object = scope:recipient
				scope:recipient = {
					OR = {
						AND = {
							#Recipient can marry
							can_marry_trigger = yes
							#To account for a person marrying their concubines
							trigger_if = {
								limit = {
									is_concubine = yes
								}
								is_concubine_of = scope:actor
								can_marry_character_trigger = { CHARACTER = scope:actor }
							}
						}
						AND = {
							is_ruler = yes #(Following are only for rulers)
							OR = {
								any_child = {
									matchmaker = scope:actor
									OR = {
										#Any of recipient's children can marry
										AND = {
											can_marry_trigger = yes
											#To account for a person marrying their concubines
											trigger_if = {
												limit = {
													is_concubine = yes
												}
												is_concubine_of = scope:actor
											}
										}
										#Any of recipient's children are ready to marry their betrothed
										AND = {
											ready_to_marry_betrothed_trigger = yes
											betrothed.matchmaker = scope:actor
										}
									}
								}
								any_courtier = {
									OR = {
										#Any of recipient's courtiers can marry
										AND = {
											can_marry_trigger = yes
											#To account for a person marrying their concubines
											trigger_if = {
												limit = {
													is_concubine = yes
												}
												is_concubine_of = scope:actor
											}
										}
										#Any of recipient's courtiers are ready to marry their betrothed
										AND = {
											ready_to_marry_betrothed_trigger = yes
											betrothed.matchmaker = scope:actor
										}
									}
								}
							}
						}
					}
				}
			}
		}
		#Diplomatic availability & prison
		scope:recipient = {
			is_busy_in_events_localised = yes
			is_imprisoned = no
		}
		NOT = {
			scope:actor = {
				is_at_war_with = scope:recipient
			}
		}
		trigger_if = { #(must be here because secondary_recipient can be set at the start)
			limit = {
				exists = scope:secondary_recipient
				NOT = { scope:secondary_recipient = scope:recipient }
			}
			scope:secondary_recipient = {
				is_busy_in_events_localised = yes
				is_imprisoned = no
				bp2_valid_for_standard_interactions_trigger = yes
			}
		}
		trigger_if = { #(must be here because secondary_recipient can be set at the start)
			limit = {
				exists = scope:secondary_actor
				NOT = { scope:secondary_actor = scope:actor }
			}
			scope:secondary_actor = {
				is_busy_in_events_localised = yes
				is_imprisoned = no
				bp2_valid_for_standard_interactions_trigger = yes
			}
		}
		# Account for regents being bolshy.
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = secondary_actor }
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = secondary_recipient }
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = actor }
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = recipient }
	}
	
	# whether a character can be picked for the interaction, interface only (e.g. picking the first character in the marriage interaction)
	# in character scope, actor and recipient event targets can be used, but not the secondary characters (because none exist yet)
	can_be_picked = {
		OR = {
			marriage_interaction_can_be_picked_trigger = yes
			AND = {
				exists = scope:secondary_actor
				exists = scope:secondary_recipient
				scope:secondary_actor.betrothed = scope:secondary_recipient
			}
		}
	}

	#Is evaluated only after secondary_recipient and secondary_actor are both chosen
	has_valid_target_showing_failures_only = {
		marriage_interaction_valid_target_trigger = yes  #Checks marriage status, betrothed, gender, consanguinity, faith hostility etc.

		#Blocks AI from marrying player guests
		scope:secondary_recipient = {
			trigger_if = {
				limit = {
					is_pool_guest = yes
					host = {
						is_ai = no
						NOT = { this = scope:actor }
					}
				}
				NOT = { is_pool_guest_of = host }
			}
		}
		
		#Blocks most AI's from marrying Evil faiths
		trigger_if = {
			limit = {
				scope:actor = {
					is_ai = yes
					NOT = { has_trait = cynical }
					#AI should honor betrothal even if faiths change
					NOT = { scope:secondary_actor.betrothed = scope:secondary_recipient }
				}
			}
			scope:recipient.faith = {
				faith_hostility_level = {
					target = scope:actor.faith
					value < faith_hostility_prevents_marriage_level
				}
			}
		}

		#Blocks AI from marrying single mayors in their area unless they are also mayors
		trigger_if = {
			limit = {
				scope:actor = {
					is_ai = yes
					NOT = { government_has_flag = government_is_republic }
				}
			}
			NOT = {
				scope:recipient = { government_has_flag = government_is_republic }
			}
		}
		
		trigger_if = { # Cultural Traditions, for AI
			limit = {
				scope:actor = {
					is_ai = yes
					culture = {
						has_cultural_parameter = dislikes_marrying_outside_of_culture
					}
				}
			}
			OR = {
				scope:actor.culture = scope:recipient.culture
				scope:actor.culture = scope:secondary_recipient.culture
			}
		}
	}

	pre_auto_accept = {
		if = {
			limit = {
				NOR = {
					scope:recipient = scope:actor
					scope:secondary_actor = { has_been_promised_grand_wedding = yes }
					scope:secondary_recipient = { has_been_promised_grand_wedding = yes }
				}
			}
			scope:recipient = {
				if = {
					limit = { scope:secondary_actor.betrothed = scope:secondary_recipient }
					send_interface_toast = {
						type = msg_betrothal_fulfilled
						left_icon = scope:secondary_actor
						right_icon = scope:secondary_recipient
					}
				}
				else = {
					send_interface_toast = {
						type = msg_marriage_forced
						left_icon = scope:secondary_actor
						right_icon = scope:secondary_recipient
					}
				}
			}
		}
	}

	on_send = {
		if = {
			limit = {
				scope:grand_wedding_promise = yes
			}
			scope:actor = { 
				set_variable = {
					name = grand_wedding_promise_pending
					days = 10
				}
				if = {
					limit = {
						is_ai = yes
					}
					remove_short_term_gold = ai_wedding_promise_gold_actor
				}
				#if = { # Activity count debugging, left in for QA purposes
				#	limit = {
				#		NOT = {
				#			exists = global_var:wedding_promise
				#		}
				#	}
				#	set_global_variable = {
				#		name = wedding_promise
				#		value = 1
				#	}
				#}
				#else = {
				#	change_global_variable = {
				#		name = wedding_promise
				#		add = 1
				#	}
				#}
			}
		}
	}

	on_accept = {
		marriage_interaction_on_accept_effect = yes
		scope:actor = {
			if = {
				limit = {
					scope:herd_send_option = yes
				}
				pay_herd = {
					target = scope:recipient
					value = scope:actor.domicile.medium_herd_value
				}
			}
			if = {
				limit = {
					has_variable = grand_wedding_promise_pending
				}
				remove_variable = grand_wedding_promise_pending
			}
			if = {
				limit = {
					is_target_in_variable_list = {
						name = contact_list_marriage
						target = scope:recipient
					}
				}
				remove_list_variable = {
					name = contact_list_marriage
					target = scope:recipient
				}
			}
		}
	}

	on_decline = {
		scope:actor = {
			if = {
				limit = { NOT = { this = scope:recipient } }
				trigger_event = marriage_interaction.0011	
			}
			if = {
				limit = {
					has_variable = grand_wedding_promise_pending
				}
				remove_variable = grand_wedding_promise_pending
			}
		}
		scope:secondary_actor = {
			add_character_flag = {
				flag = player_declined_marriage
				years = 5
			}
		}
	}

	auto_accept = {
		marriage_interaction_auto_accept_trigger = yes #Strong hook conditions
	}

	ai_accept = {
		base = 0

		marriage_ai_accept_modifier = yes
	}

	# If you want the AI to set these options then it needs to be hooked into the code as well
	send_option = {
		flag = grand_wedding_promise
		localization = GRAND_WEDDING_PROMISE

		is_shown = {
			scope:actor = { #Can organize a Grand Wedding
				highest_held_title_tier > tier_barony
				is_landed_or_landless_administrative = yes
				trigger_if = {
					limit = {
						is_ai = yes
					}
					is_at_war = no
				}
				# DLC locked
				has_dlc_feature = tours_and_tournaments
				has_dlc_feature = advanced_activities
			}
		}

		current_description = {
			desc = "GRAND_WEDDING_PROMISE_INFO"
		}

		is_valid = {
			scope:actor = {
				gold >= real_wedding_cost_actor_scope
				is_available = yes
				custom_tooltip = {
					text = grand_wedding_already_promised_tt
					NOR = {
						AND = {
							has_variable = promised_grand_wedding_marriage_countdown
							var:promised_grand_wedding_marriage_countdown = { is_alive = yes }
						}
						has_variable = grand_wedding_promise_pending
					}
				}
			}
			trigger_if = {
				limit = {
					exists = scope:secondary_actor
				}
				custom_tooltip = {
					text = grand_wedding_only_dominant_side
					trigger_if = {
						limit = {
							scope:secondary_actor ?= {
								is_female = yes
							}
						}
						OR = {
							scope:secondary_actor ?= { matrilinear_betrothal = yes }
							scope:matrilineal = yes
						}
					}
					trigger_else = {
						NOR = {
							scope:secondary_actor ?= { matrilinear_betrothal = yes }
							scope:matrilineal = yes
						}
					}
				}
				custom_tooltip = {
					text = grand_wedding_only_family
					scope:secondary_actor ?= {
						is_close_or_extended_family_of = scope:actor
					}
				}
				# Cannot promise grand weddings with regents due to regency and travel limitations
				custom_tooltip = {
					text = grand_wedding_secondary_regent
					NOT = {
						scope:secondary_actor.liege = {
							diarch = scope:secondary_actor
							has_active_diarchy = yes
						}
					}
				}
			}
			# Safety check because players shouldn't be spouse_2
			trigger_if = {
				limit = {
					has_multiple_players = yes
				}
				custom_tooltip = {
					text = grand_wedding_mp_ai
					trigger_if = {
						limit = {
							OR = {
								scope:actor = { is_ai = no }
								scope:secondary_actor ?= { is_ai = no }
							}
						}
						scope:secondary_recipient ?= {
							is_ai = yes
						}
					}
				}
			}
			trigger_if = {
				limit = {
					exists = scope:secondary_recipient.liege
					scope:secondary_recipient.liege = {
						diarch = scope:secondary_recipient
						has_active_diarchy = yes
					}
				}
				# Cannot promise grand weddings with regents due to regency and travel limitations
				custom_tooltip = {
					text = grand_wedding_secondary_regent
					NOT = {
						scope:secondary_recipient.liege = {
							diarch = scope:secondary_recipient
							has_active_diarchy = yes
						}
					}
				}
			}
		}
	}

	send_option = {
		flag = matrilineal
		localization = MATRILINEAL_OFFER
		is_shown = {
			OR = {
				NOT = { exists = scope:secondary_actor }
				NOT = { exists = scope:secondary_recipient }
				scope:secondary_actor = { sex_opposite_of = scope:secondary_recipient }
			}
		}
		current_description = {
			triggered_desc = {
				trigger = {
					exists = scope:secondary_actor
					scope:secondary_actor = {
						betrothed = scope:secondary_recipient
						matrilinear_betrothal = yes
					}
				}
				desc = "MATRILINEAL_OFFER_BETROTHAL_INFO"
			}
		}
		can_be_changed = {
			trigger_if = {
				limit = {
					exists = scope:secondary_actor
				}
				OR = {
					NOT = {
						exists = scope:secondary_recipient
					}
					scope:secondary_actor = {
						NOT = {
							betrothed = scope:secondary_recipient
						}
					}
				}
			}
		}
		starts_enabled = {
			trigger_if = {
				limit = {
					exists = scope:secondary_actor
					scope:secondary_actor = {
						betrothed = scope:secondary_recipient
					}
				}
				scope:secondary_actor = {
					matrilinear_betrothal = yes
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:secondary_actor
					scope:actor = scope:secondary_actor
					is_female = yes
					is_ai = no
				}
				always = yes
			}
			trigger_else = {
				scope:actor = {
					OR= {
						faith = {
							has_doctrine = doctrine_gender_female_dominated
						}
						AND = {
							this = scope:recipient
							exists = scope:secondary_recipient
							scope:secondary_recipient = {
								is_female = yes
							}
							faith = { has_doctrine = doctrine_gender_equal }
						}
						AND = {
							exists = scope:secondary_actor
							scope:actor = scope:secondary_actor
							is_female = yes
							is_ai = no
						}
					}
				}
			}
		}
	}	

	send_option = {
		is_shown = {
			scope:secondary_actor = {
				NOT = {
					betrothed = scope:secondary_recipient
				}
			}
		}
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = MARRIAGE_HOOK
	}

	send_option = { # FP3
		is_shown = {
			scope:actor = {
				any_character_struggle = {
					is_struggle_type = persian_struggle
					has_struggle_phase_parameter = can_trade_piety_for_marriage_acceptance
				}
			}
		}
		is_valid = {
			scope:actor = {
				any_character_struggle = {
					involvement = involved
					is_struggle_type = persian_struggle
				}
			}
			scope:recipient = {
				any_character_struggle = {
					involvement = involved
					is_struggle_type = persian_struggle
				}
			}
		}
		flag = piety_cost_reduction
		localization = TRADE_PIETY_FOR_BETTER_AI_ACCEPTANCE
	}

	send_option = { # EP3 Influence
		is_shown = { # Actor must have a government that uses influence 
			scope:actor = {
				government_has_flag = government_has_influence
			}
		}
		is_valid = {
			# Actor has enough influence
			scope:actor = { influence >= medium_influence_value }
			# Both characters are within the same top realm
			custom_tooltip = {
				text = not_same_realm_tt
				scope:recipient.top_liege = scope:actor.top_liege
			}
		}
		flag = influence_send_option
		localization = TRADE_INFLUENCE_FOR_BETTER_AI_ACCEPTANCE
	}
	
	send_option = { # MPO Herd
		is_shown = { # Actor must have a government that uses Herd 
			scope:actor = {
				government_has_flag = government_is_nomadic
				is_ai = no
			}
			# Recipient must care about Herd
			scope:recipient = {
				government_has_flag = government_is_nomadic
			}
		}
		is_valid = {
			# Actor has enough Herd
			scope:actor = { domicile.herd >= domicile.medium_herd_value }
		}
		flag = herd_send_option
		localization = TRADE_HERD_FOR_BETTER_AI_ACCEPTANCE
	}

	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"
	
	send_options_exclusive = no
	
	show_effects_in_notification = no
	greeting = positive
	
	notification_text = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:secondary_actor = {
						is_adult = yes
					}
					scope:secondary_recipient = {
						is_adult = yes
					}
					NOR = {
						scope:secondary_actor = { has_been_promised_grand_wedding = yes }
						scope:secondary_recipient = { has_been_promised_grand_wedding = yes }
					}
				}
				desc = {
					first_valid = {	
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_actor = scope:actor
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_BOTH_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_actor = scope:actor
							}
							desc = MARRIAGE_NOTIFICATION_ACTOR_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_RECIPIENT_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
							}
							desc = MARRIAGE_NOTIFICATION_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:secondary_actor = scope:actor
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_BOTH_PERSONAL
						}
						triggered_desc = {
							trigger = {
								scope:secondary_actor = scope:actor
							}
							desc = MARRIAGE_NOTIFICATION_ACTOR_PERSONAL
						}
						triggered_desc = {
							trigger = {
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_RECIPIENT_PERSONAL
						}
						desc = MARRIAGE_NOTIFICATION
					}
				}
			}
			desc = {
				first_valid = {	
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_actor = scope:actor
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_BOTH_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_actor = scope:actor
						}
						desc = BETROTHAL_NOTIFICATION_ACTOR_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_RECIPIENT_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
						}
						desc = BETROTHAL_NOTIFICATION_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:secondary_actor = scope:actor
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_BOTH_PERSONAL
					}
					triggered_desc = {
						trigger = {
							scope:secondary_actor = scope:actor
						}
						desc = BETROTHAL_NOTIFICATION_ACTOR_PERSONAL
					}
					triggered_desc = {
						trigger = {
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_RECIPIENT_PERSONAL
					}
					desc = BETROTHAL_NOTIFICATION
				}
			}
		}
	}
}

#This interaction is referenced in code! If it's renamed you have to ping a coder
marry_off_interaction = {
	icon = icon_marriage
	category = interaction_category_diplomacy
	common_interaction = yes

	desc = marry_off_interaction_desc
	
	send_name = marry_off_send
	needs_recipient_to_open = no
	special_interaction = arrange_marriage_interaction
	interface = marriage
	redirect = {
		if = {
			limit = {
				exists = scope:secondary_actor
			}
			scope:recipient = {
				save_scope_as = secondary_recipient
				matchmaker = {
					save_scope_as = recipient
				}
			}

		}
		else = {
			scope:recipient = {
				save_scope_as = secondary_actor
			}
			clear_saved_scope = recipient
		}
	}

	populate_recipient_list = {
		#populate only with your courtiers or children/grandchildren if you are a landless adventurer
		if = {
			limit = {
				scope:actor = {
					is_landless_adventurer = yes
				}
			}
			scope:secondary_actor = {
				if = {
					limit = {
						NOT = { scope:actor = this }
					}
					add_to_list = characters
				}
				every_courtier = {
					limit = {
						trigger_if = {
							limit = {
								is_concubine = yes
							}
							is_concubine_of = scope:actor
							can_marry_character_trigger = { CHARACTER = scope:actor }
						}
					}
					add_to_list = characters
				}
				every_courtier_away = {
					limit = {
						trigger_if = {
							limit = {
								is_concubine = yes
							}
							is_concubine_of = scope:actor
							can_marry_character_trigger = { CHARACTER = scope:actor }
						}
					}
					add_to_list = characters
				}
				every_child = {
					even_if_dead = yes
					if = {
						limit = {
							is_alive = yes
							NOT = { is_in_list = characters }
							trigger_if = {
								limit = {
									is_concubine = yes
								}
								is_concubine_of = scope:actor
								can_marry_character_trigger = { CHARACTER = scope:actor }
							}
						}
						add_to_list = characters
					}
					every_child = {
						even_if_dead = yes
						if = {
							limit = {
								is_alive = yes
								NOT = { is_in_list = characters }
								trigger_if = {
									limit = {
										is_concubine = yes
									}
									is_concubine_of = scope:actor
									can_marry_character_trigger = { CHARACTER = scope:actor }
								}
							}
							add_to_list = characters
						}
						every_child = {
							if = {
								limit = {
									is_alive = yes
									NOT = { is_in_list = characters }
									trigger_if = {
										limit = {
											is_concubine = yes
										}
										is_concubine_of = scope:actor
										can_marry_character_trigger = { CHARACTER = scope:actor }
									}
								}
								add_to_list = characters
							}
						}
					}
				}
			}
		}
		#otherwise, populate with anyone eligible
		else = {
			scope:secondary_actor = {
				if = {
					limit = {
						allowed_to_marry_same_sex_trigger = yes
					}
					every_opposite_sex_spouse_candidate = {
						add_to_list = characters
					}
					every_same_sex_spouse_candidate = {
						limit = {
							NOT = { scope:secondary_actor = this }
						}
						add_to_list = characters
					}
				}
				else = {
					every_opposite_sex_spouse_candidate = {
						add_to_list = characters
					}
				}
			}
		}
	}

	cost = {
		prestige = {
			value = 0
			if = {
				limit = {
					exists = scope:actor
					exists = scope:recipient
					exists = scope:secondary_recipient
					scope:recipient = {
						OR = {
							AND = {
								vassal_contract_has_flag = vassal_contract_marriage_favor
								is_allied_to = scope:actor
							}
							NOT = { vassal_contract_has_flag = vassal_contract_marriage_favor}
						}
					}
				}
				add = {
					scope:actor = {
						every_vassal = {
							limit = {
								vassal_contract_has_flag = vassal_contract_marriage_favor
								NOT = { is_allied_to = scope:actor }
								NOR = {
									this = scope:recipient
									this = scope:secondary_recipient
								}
							}
							add = medium_prestige_value
						}
					}
					desc = VASSAL_WITH_MARRIAGE_FAVOR
				}
			}
		}

		influence = {
			value = 0
			if = {
				limit = { scope:influence_send_option = yes }
				add = scope:actor.medium_influence_value
				desc = INFLUENCE_INTERACTION_ACCEPTANCE_SEND_OPTION
			}
		}
	}

	interface_priority = 60

	# actor, recipient ... characters arranging the marriage
	# secondary_actor, secondary_recipient ... characters that should marry

	is_shown = {
		# Enable only for yourself if you're a landless adventurer
		trigger_if = {
			limit = {
				scope:actor = {
					is_landless_adventurer = yes
				}
			}
			scope:secondary_actor = {
				scope:actor = this
			}
		}
		scope:secondary_actor = {
			#Is this someone I can marry off?
			OR = {
				is_courtier_of = scope:actor
				AND = {
					is_child_of = scope:actor
					target_is_liege_or_above = scope:actor
				}
				scope:actor = this
			}
			#They can be married
			can_marry_trigger = yes
			#To account for a person marrying their concubines
			trigger_if = {
				limit = {
					is_concubine = yes
				}
				is_concubine_of = scope:actor
				can_marry_character_trigger = { CHARACTER = scope:actor }
			}
		}
		# Block further marriage proposals for people who have a pending grand wedding
		NOR = {
			scope:secondary_actor ?= { has_been_promised_grand_wedding = yes }
			scope:secondary_recipient ?= { has_been_promised_grand_wedding = yes }
		}
	}

	is_valid_showing_failures_only = {
		#Diplomatic availability & prison
		scope:secondary_actor = {
			is_busy_in_events_localised = yes
			is_imprisoned = no
			bp2_valid_for_standard_interactions_trigger = yes
		}
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = secondary_actor }
		trigger_if = {
			limit = {
				NOT = { scope:secondary_actor = scope:secondary_recipient }
			}
			marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = secondary_recipient }
		}
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = actor }
		marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = { TARGET = recipient }
	}

	has_valid_target_showing_failures_only = {
		marriage_interaction_valid_target_trigger = yes  #Checks marriage status, betrothed, gender, consanguinity, faith hostility etc.

		#Diplomatic availability & prison (located here because these will never be set at the start)
		scope:recipient = {
			is_busy_in_events_localised = yes
			is_imprisoned = no
		}
		NOT = {
			scope:actor = {
				is_at_war_with = scope:recipient
			}
		}
		trigger_if = {
			limit = { NOT = { scope:recipient = scope:secondary_recipient } }
			scope:secondary_recipient = {
				is_busy_in_events_localised = yes
				is_imprisoned = no
				bp2_valid_for_standard_interactions_trigger = yes
			}
		}
		#secondary_actor is checked above because they are always available

		#Blocks AI from marrying player guests
		scope:secondary_recipient = {
			trigger_if = {
				limit = {
					is_pool_guest = yes
					host = {
						is_ai = no
						NOT = { this = scope:actor }
					}
				}
				NOT = { is_pool_guest_of = host }
			}
		}
	}

	on_send = {
		if = {
			limit = {
				scope:grand_wedding_promise = yes
			}
			scope:actor = { 
				set_variable = {
					name = grand_wedding_promise_pending
					days = 10
				}
				if = {
					limit = {
						is_ai = yes
					}
					remove_short_term_gold = ai_wedding_promise_gold_actor
				}
				#if = { # Activity count debugging, left in for QA purposes
				#	limit = {
				#		NOT = {
				#			exists = global_var:wedding_promise
				#		}
				#	}
				#	set_global_variable = {
				#		name = wedding_promise
				#		value = 1
				#	}
				#}
				#else = {
				#	change_global_variable = {
				#		name = wedding_promise
				#		add = 1
				#	}
				#}
			}
		}
	}

	on_accept = {
		marriage_interaction_on_accept_effect = yes	

		scope:actor = { 
			if = {
				limit = {
					scope:herd_send_option = yes
				}
				pay_herd = {
					target = scope:recipient
					value = scope:actor.domicile.medium_herd_value
				}
			}
			if = {
				limit = {
					has_variable = grand_wedding_promise_pending
				}
				remove_variable = grand_wedding_promise_pending
			}
		}
	}

	on_decline = {
		scope:actor = {
			if = {	
				limit = { NOT = { this = scope:recipient } }
				trigger_event = marriage_interaction.0011	
			}
			if = {
				limit = {
					has_variable = grand_wedding_promise_pending
				}
				remove_variable = grand_wedding_promise_pending
			}
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_loss
			DESC = clan_unity_marry_off_decline.desc
			REVERSE_NON_HOUSE_TARGET = no
		}		
	}

	auto_accept = {
		marriage_interaction_auto_accept_trigger = yes #Strong hook conditions
	}
	
	ai_accept = {
		base = 0 # Should be 0 for all interactions
		
		marriage_ai_accept_modifier = yes

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}
	}

	send_option = {
		flag = grand_wedding_promise
		localization = GRAND_WEDDING_PROMISE

		is_shown = {
			scope:actor = { #Can organize a Grand Wedding
				highest_held_title_tier > tier_barony
				is_landed_or_landless_administrative = yes
				trigger_if = {
					limit = {
						is_ai = yes
					}
					is_at_war = no
				}
				# DLC locked
				has_dlc_feature = tours_and_tournaments
				has_dlc_feature = advanced_activities
			}
		}

		current_description = {
			desc = "GRAND_WEDDING_PROMISE_INFO"
		}

		is_valid = {
			scope:actor = {
				gold >= real_wedding_cost_actor_scope
				is_available = yes
				custom_tooltip = {
					text = grand_wedding_already_promised_tt
					NOR = {
						AND = {
							has_variable = promised_grand_wedding_marriage_countdown
							var:promised_grand_wedding_marriage_countdown = { is_alive = yes }
						}
						has_variable = grand_wedding_promise_pending
					}
				}
			}
			custom_tooltip = {
				text = grand_wedding_only_dominant_side
				trigger_if = {
					limit = {
						scope:secondary_actor ?= {
							is_female = yes
						}
					}
					OR = {
						scope:secondary_actor ?= { matrilinear_betrothal = yes }
						scope:matrilineal = yes
					}
				}
				trigger_else = {
					NOR = {
						scope:secondary_actor ?= { matrilinear_betrothal = yes }
						scope:matrilineal = yes
					}
				}
			}
			custom_tooltip = {
				text = grand_wedding_only_family
				scope:secondary_actor ?= {
					is_close_or_extended_family_of = scope:actor
				}
			}
		}
	}
	
	send_option = {
		flag = matrilineal
		localization = "MATRILINEAL_OFFER"
		starts_enabled = {
			scope:actor = {
				OR = {
					faith = {
						has_doctrine = doctrine_gender_female_dominated
					}
					AND = {
						scope:secondary_actor = { is_female = yes }
						faith = { has_doctrine = doctrine_gender_equal }
					}
					AND = {
						exists = scope:secondary_actor
						scope:actor = scope:secondary_actor
						is_female = yes
						is_ai = no
					}
				}
			}
		}
	}
	
	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = MARRIAGE_HOOK
	}
	#This option cannot be linked with the hook icon, because recipient isn't known in the right-click menu

	send_option = { # EP3 Influence
		is_shown = { # Actor must have a government that uses influence 
			scope:actor = {
				government_has_flag = government_has_influence
			}
		}
		is_valid = {
			# Actor has enough influence
			scope:actor = { influence >= medium_influence_value }
			# Both characters are within the same top realm
			custom_tooltip = {
				text = not_same_realm_tt
				scope:recipient.top_liege = scope:actor.top_liege
			}
		}
		flag = influence_send_option
		localization = TRADE_INFLUENCE_FOR_BETTER_AI_ACCEPTANCE
	}
	send_option = { # MPO Herd
		is_shown = { # Actor must have a government that uses Herd 
			scope:actor = {
				government_has_flag = government_is_nomadic
				is_ai = no
			}
			# Recipient must care about Herd
			scope:recipient = {
				government_has_flag = government_is_nomadic
			}
		}
		is_valid = {
			# Actor has enough Herd
			scope:actor = { domicile.herd >= domicile.medium_herd_value }
		}
		flag = herd_send_option
		localization = TRADE_HERD_FOR_BETTER_AI_ACCEPTANCE
	}
	
	send_options_exclusive = no
	
	show_effects_in_notification = no
	greeting = positive
	
	notification_text = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:secondary_actor = {
						is_adult = yes
					}
					scope:secondary_recipient = {
						is_adult = yes
					}
					NOR = {
						scope:secondary_actor = { has_been_promised_grand_wedding = yes }
						scope:secondary_recipient = { has_been_promised_grand_wedding = yes }
					}
				}
				desc = {
					first_valid = {	
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_actor = scope:actor
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_BOTH_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_actor = scope:actor
							}
							desc = MARRIAGE_NOTIFICATION_ACTOR_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_RECIPIENT_PERSONAL_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:matrilineal = yes
							}
							desc = MARRIAGE_NOTIFICATION_MATRI
						}
						triggered_desc = {
							trigger = {
								scope:secondary_actor = scope:actor
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_BOTH_PERSONAL
						}
						triggered_desc = {
							trigger = {
								scope:secondary_actor = scope:actor
							}
							desc = MARRIAGE_NOTIFICATION_ACTOR_PERSONAL
						}
						triggered_desc = {
							trigger = {
								scope:secondary_recipient = scope:recipient
							}
							desc = MARRIAGE_NOTIFICATION_RECIPIENT_PERSONAL
						}
						desc = MARRIAGE_NOTIFICATION
					}
				}
			}
			desc = {
				first_valid = {	
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_actor = scope:actor
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_BOTH_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_actor = scope:actor
						}
						desc = BETROTHAL_NOTIFICATION_ACTOR_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_RECIPIENT_PERSONAL_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:matrilineal = yes
						}
						desc = BETROTHAL_NOTIFICATION_MATRI
					}
					triggered_desc = {
						trigger = {
							scope:secondary_actor = scope:actor
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_BOTH_PERSONAL
					}
					triggered_desc = {
						trigger = {
							scope:secondary_actor = scope:actor
						}
						desc = BETROTHAL_NOTIFICATION_ACTOR_PERSONAL
					}
					triggered_desc = {
						trigger = {
							scope:secondary_recipient = scope:recipient
						}
						desc = BETROTHAL_NOTIFICATION_RECIPIENT_PERSONAL
					}
					desc = BETROTHAL_NOTIFICATION
				}
			}
		}
	}
}

#New "make concubine" interaction.
make_concubine_interaction = {
	category = interaction_category_diplomacy
	icon = concubine_icon

	desc = make_concubine_interaction_desc

	interface = concubine_list
	

	needs_recipient_to_open = yes

	redirect = {
		# The UI will open to the already filled left and right first if we don't tell it we've got both sides filled
		scope:actor = {
			save_scope_as = secondary_actor
		}
		scope:recipient = {
			save_scope_as = secondary_recipient
		}
	}

	populate_recipient_list = {
		scope:actor = {
			every_courtier = {
				limit = {
					can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
					can_be_taken_as_muslim_concubine_trigger = {  # Can a Muslim be taken as concubine?
						CHARACTER = scope:actor 
						RECEIVER = scope:actor
					}
				}
				add_to_list = characters
			}
			every_prisoner = {
				limit = {
					NOT = { is_in_list = characters }
					can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
				}
				add_to_list = characters
			}
		}
	}
	
	is_shown = {
		make_concubine_is_shown_trigger = yes
	}

	is_valid_showing_failures_only = {
		make_concubine_is_valid_trigger = yes
	}
	
	auto_accept = yes
	
	on_accept = {
		concubine_on_accept_effect = yes
	}
}

#Now for AI
ai_make_concubine_interaction = {
	hidden = yes
	
	ai_targets = {
		ai_recipients = prisoners
	}
	ai_targets = {
		ai_recipients = courtiers
	}
	ai_target_quick_trigger = {
		adult = yes
		owner_attracted = yes # Not optimal, but saves a lot of performance
	}
	ai_frequency = 12

	ai_potential = {
		is_playable_character = yes
		is_adult = yes
		fertility > 0
		allowed_more_concubines = yes
		NOR = {
			has_trait = chaste
			has_trait = celibate
			has_sexuality = asexual
		}
	}

	is_shown = {
		make_concubine_is_shown_trigger = yes
	}

	is_valid_showing_failures_only = {
		make_concubine_is_valid_trigger = yes
	}

	auto_accept = yes

	ai_will_do = {
		base = 0
		
		modifier = { # Basic Filtering
			add = 100
			scope:actor = {
				number_of_concubines < number_of_desired_concubines
			}
			scope:recipient = {
				can_have_children_with = { CHARACTER = scope:actor } # Only if you can have kids since this is about fertility and genetic traits
				fertility > 0.2
				is_young_character = yes
			}
		}

		modifier = { # Good genetic traits
			add = 15
			scope:recipient = {
				num_of_good_genetic_traits >= 1
			}
		}
		modifier = { # Multiple good genetic traits
			add = 15
			scope:recipient = {
				num_of_good_genetic_traits > 1
			}
		}
		modifier = { # Bad genetic traits
			add = -30
			scope:recipient = {
				num_of_bad_genetic_traits >= 1
			}
		}
		modifier = { # Multiple bad genetic traits
			add = -30
			scope:recipient = {
				num_of_bad_genetic_traits > 1
			}
		}
		#Traits
		modifier = {
			add = 15
			scope:actor = {
				has_trait = lustful
			}
		}

		modifier = { # Lovers and rivals
			add = 200
			scope:actor = {
				NOR = { 
					relation_with_character_is_incestuous_in_my_or_lieges_faith_trigger = { CHARACTER = scope:recipient } 
					relation_with_character_is_sodomy_in_my_or_lieges_faith_trigger = { CHARACTER = scope:recipient }
				}
			}
			scope:recipient = {
				OR = {
					#Don't take a lover who doesn't want to be a concubine as a concubine unless your relationship with them is already *complicated*
					AND = {	
						has_relation_lover = scope:recipient
						hp_accepts_concubinage = yes
					}
					has_relation_rival = scope:actor
				}
				is_young_character = yes
			}
		}
		
		modifier = {
			add = 100
			scope:recipient = {
				is_imprisoned_by = scope:actor
				can_have_children_with = { CHARACTER = scope:actor } # Only if you can have kids since this is about fertility and genetic traits
				OR = {
					is_lowborn = no
					num_of_good_genetic_traits > 0
				}
				fertility > 0.1
				trigger_if = {
					limit = {
						is_female = yes
					}
					is_aging_character = no
				}
			}
		}

		modifier = {
			add = 100
			scope:actor = {
				allowed_to_marry_same_sex_trigger = yes
				sex_same_as = scope:recipient
				is_attracted_to_gender_of = scope:recipient
				OR = { #If they're not in need of kids, or someone who they can have kids with
					any_child = {
						count >= 2
					}
					any_consort = {
						can_have_children_with = { CHARACTER = scope:actor }
						fertility > 0.1
					}
				}
			}
		}

		modifier = { #Even if you're allowed to take same-sex concubines we don't want the AI to do so without a good reason if they're not attracted to the person
			add = -200
			scope:actor = {
				allowed_to_marry_same_sex_trigger = yes
				sex_same_as = scope:recipient
				NOT = { is_attracted_to_gender_of = scope:recipient }
			}
		}

		modifier = { # If they don't have a batch of kids and opposite sex consorts to handle that situation
			add = -200
			scope:actor = {
				allowed_to_marry_same_sex_trigger = yes
				sex_same_as = scope:recipient
				NOT = {
					any_child = {
						count >= 2
					}
				}
				NOT = {
					any_consort = {
						can_have_children_with = { CHARACTER = scope:actor }
						fertility > 0.1
					}
				}
			}
		}

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}

		modifier = { # Incest Filtering: Hard NO!
			factor = 0
			scope:actor = {
				relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:recipient }
				NOR = {
					accepts_incest_with_trigger = { CHARACTER = scope:recipient }
					sexually_liberal_trigger = yes
					AND = {
						AND = {
							exists = primary_heir
							primary_heir = { NOT = { dynasty = root.dynasty }	}
						}
						NOT = {
							primary_title = {
								any_title_heir = {	dynasty = root.dynasty 	}
							}
						}
					}
				}
			}
		}

		modifier = { # Incest Filtering: Not likely, but keep the door open...
			factor = 0.2 
			scope:actor = {
				relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:recipient }
				OR = {
					accepts_incest_with_trigger = { CHARACTER = scope:recipient }
					sexually_liberal_trigger = yes
					AND = {
						exists = primary_heir
						primary_heir = { NOT = { dynasty = root.dynasty }	}
						NOT = {
							primary_title = {
								any_title_heir = {	dynasty = root.dynasty 	}
							}
						}
					}
				}
			}
		}

		modifier = { # Less likely if married to a player and the target is the player heir 
			factor = 0.4
			scope:actor = {
				any_spouse = {
					is_ai = no
					scope:recipient = {	
						OR = {
							is_primary_heir_of = prev 
							is_heir_of = prev
						}
					}
				}
			}
		}
	}

	on_accept = {
		scope:actor = {
			save_scope_as = secondary_actor
		}
		scope:recipient = {
			save_scope_as = secondary_recipient
		}
		concubine_on_accept_effect = yes
	}
}

#Now for find a concubine
find_concubine = {
	category = interaction_category_diplomacy
	icon = concubine_icon

	desc = find_concubine_desc

	interface = concubine_list

	needs_recipient_to_open = no

	redirect = {
		scope:actor = { # The UI will open to the already filled left first if we don't tell it we've got "our side" actor which is always ourselves
			save_scope_as = secondary_actor
		}
	}

	populate_recipient_list = {
		scope:actor = {
			every_courtier = {
				limit = {
					can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
					can_be_taken_as_muslim_concubine_trigger = {  # Can a Muslim be taken as concubine?
						CHARACTER = scope:actor 
						RECEIVER = scope:actor
					}
				}
				add_to_list = characters
			}
			every_prisoner = {
				limit = {
					NOT = { is_in_list = characters }
					can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
				}
				add_to_list = characters
			}
		}
	}

	is_shown = {
		scope:actor = {
			allowed_concubines = yes
			is_adult = yes
			trigger_if = { #So that we show the recipient list, but can also only use this on ourselves, we have to do this check in the beginning
				limit = {
					NOT = { exists = scope:secondary_recipient }
				}
				scope:actor = scope:recipient
			}
		}
		trigger_if = {
			limit = {
				NOT = { scope:actor = scope:recipient }
			}
			scope:actor = {
				trigger_if = {
					limit = {
						allowed_to_marry_same_sex_trigger = no
					}
					sex_opposite_of = scope:recipient
				}
				NOT = { relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:recipient } }
			}
			scope:recipient = {
				can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
				can_be_taken_as_muslim_concubine_trigger = {  # Can a Muslim be taken as concubine?
					CHARACTER = scope:actor 
					RECEIVER = scope:actor
				}
				is_adult = yes
				OR = {
					is_imprisoned_by = scope:actor
					is_courtier_of = scope:actor
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		custom_description = {
			text = take_concubine_allowed_more
			object = scope:recipient
			scope:actor = {
				allowed_more_concubines = yes
			}
		}
		custom_description = {
			text = take_concubine_available_character_opp
			object = scope:actor
			scope:actor = {
				OR = {
					any_courtier = {
						can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
						can_be_taken_as_muslim_concubine_trigger = {  # Can a Muslim be taken as concubine?
							CHARACTER = scope:actor 
							RECEIVER = scope:actor
						}
					}
					any_prisoner = {
						can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
					}
				}
			}
		}
		trigger_if = {
			limit = {
				NOT = { scope:actor = scope:recipient }
			}
			scope:recipient = {
				can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
				can_be_taken_as_muslim_concubine_trigger = {  # Can a Muslim be taken as concubine?
					CHARACTER = scope:actor 
					RECEIVER = scope:actor
				}
			}
		}
	}

	can_be_picked = {
		is_adult = yes
	}

	auto_accept = yes

	ai_will_do = {
		base = 0
	}
	
	on_accept = {
		concubine_on_accept_effect = yes
	}
}

#To account for a dismissed Grand Consort
dismiss_concubine_interaction = {
	category = interaction_category_diplomacy
	use_diplomatic_range = no	
	icon = concubine_icon
	
	is_shown = {
		scope:recipient = {
			is_concubine_of = scope:actor
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = { NOT = { has_strong_hook = scope:actor } }
		scope:actor = {
			custom_tooltip = {
				NOT = {
					var:family_widow_forbidden_divorce ?= scope:target
				}
				text = BOUND_TO_FAMILY_WIDOW
			}
		}
	}

	auto_accept = yes
	
	on_accept = {	
		calyver_dismiss_concubine_effect = yes	
	}
}

#You can make your former spouse a concubine
divorce_character_interaction = {
	category = interaction_category_diplomacy
	use_diplomatic_range = no
	icon = icon_marriage

	desc = divorce_character_interaction_desc
	cost = { 
		piety = divorce_cost 
		influence = {
			value = 0
			if = {
				limit = { 
					scope:actor = {
						government_has_flag = government_has_influence 
					}
				}
				if = { #If recipient comes from a powerful family or the same rank, some influence
					limit = {
						scope:recipient = {
							any_close_family_member = {
								NOR = { 
									this = scope:actor 
									is_child_of = scope:actor
								}
								OR = {
									is_powerful_vassal_of = scope:actor
									is_councillor_of = scope:actor
									tier_difference = {
										target = scope:actor
										value = 0
									}
									AND = { #Or belongs to the same realm
										government_has_flag = government_has_influence
										exists = scope:actor.top_liege
										exists = scope:actor.house
										top_liege ?= {
											this = scope:actor.top_liege
										}
										house ?= {
											is_powerful_family = yes
											NOT = { this = scope:actor.house }
											is_dominant_family = no
										}
									}
								}
							}
						}
					}
					add = medium_influence_value
				}
				if = { #If recipient comes from a dominant family, massive influence
					limit = {
						scope:recipient = {
							any_close_family_member = {
								NOR = { 
									this = scope:actor 
									is_child_of = scope:actor
								}
								OR = {
									tier_difference = {
										target = scope:actor
										value > 0
									}
									AND = { #Or belongs to the same realm
										government_has_flag = government_has_influence
										OR = {
											AND = {
												exists = scope:actor.top_liege
												top_liege ?= {
													this = scope:actor.top_liege
												}
											}
											is_vassal_or_below_of = scope:actor
										}
										exists = scope:actor.house
										house ?= {
											NOT = { this = scope:actor.house }
											is_dominant_family = yes
										}
									}
								}
							}
						}
					}
					add = {
						value = major_influence_value
						if = {
							limit = { #More for someone who far outranks you
								scope:recipient = {
									any_close_family_member = {
										NOR = { 
											this = scope:actor 
											is_child_of = scope:actor
										}
										tier_difference = {
											target = scope:actor
											value > 1
										}
									}
								}
							}
							add = major_influence_value
						}
					}
				}
			}
		}
		prestige = {
			value = 0
			if = {
				limit = { 
					NOT = { 
						scope:actor = {
							government_has_flag = government_has_influence 
						}
					} 
				}
				if = { #If recipient comes from a powerful family or the same rank, some prestige
					limit = {
						scope:recipient = {
							any_close_family_member = {
								NOR = { 
									this = scope:actor 
									is_child_of = scope:actor
								}
								OR = {
									is_powerful_vassal_of = scope:actor
									is_councillor_of = scope:actor
									tier_difference = {
										target = scope:actor
										value = 0
									}
									AND = { #Or belongs to the same realm
										government_has_flag = government_has_influence
										exists = scope:actor.top_liege
										exists = scope:actor.house
										top_liege ?= {
											this = scope:actor.top_liege
										}
										house ?= {
											is_powerful_family = yes
											NOT = { this = scope:actor.house }
											is_dominant_family = no
										}
									}
								}
							}
						}
					}
					add = medium_prestige_value
				}
				if = { #If recipient comes from a dominant family, massive prestige
					limit = {
						scope:recipient = {
							any_close_family_member = {
								NOR = { 
									this = scope:actor 
									is_child_of = scope:actor
								}
								OR = {
									tier_difference = {
										target = scope:actor
										value > 0
									}
									AND = { #Or belongs to the same realm
										government_has_flag = government_has_influence
										OR = {
											AND = {
												exists = scope:actor.top_liege
												top_liege ?= {
													this = scope:actor.top_liege
												}
											}
											is_vassal_or_below_of = scope:actor
										}
										exists = scope:actor.house
										house ?= {
											NOT = { this = scope:actor.house }
											is_dominant_family = yes
										}
									}
								}
							}
						}
					}
					add = {
						value = major_prestige_value
						if = {
							limit = { #More for someone who far outranks you
								scope:recipient = {
									any_close_family_member = {
										NOR = { 
											this = scope:actor 
											is_child_of = scope:actor
										}
										tier_difference = {
											target = scope:actor
											value > 1
										}
									}
								}
							}
							add = major_prestige_value
						}
					}
				}
			}
		}
	}

	is_shown = {
		scope:actor.faith = {
			# We are not required to seek approval of a spiritual HoF
			NAND = {
				has_doctrine = doctrine_divorce_approval
				has_doctrine = doctrine_spiritual_head
				exists = religious_head
			}
			# We are not required to seek approval of a house head
			NAND = {
				has_doctrine = doctrine_divorce_approval
				exists = scope:actor.house
				exists = scope:actor.house.house_head
				scope:actor = { is_house_head = no }
			}
		}
		scope:recipient = {
			is_spouse_of = scope:actor
			is_concubine = no # Can set aside Concubines at any time
		}
	}

	is_valid_showing_failures_only = {
		custom_tooltip = {
			NOT = {
				scope:actor.culture = {
					has_cultural_parameter = cannot_divorce
				}
			}
			text = CULTURE_FORBIDS_DIVORCE
		}
		NOT = {
			scope:actor.faith = {
				has_doctrine = doctrine_divorce_disallowed
			}
		}
		scope:recipient = { is_busy_in_events_localised = yes }
		scope:recipient = { NOT = { has_strong_hook = scope:actor } }
		scope:actor = {
			custom_tooltip = {
				NOT = {
					var:family_widow_forbidden_divorce ?= scope:target
				}
				text = BOUND_TO_FAMILY_WIDOW
			}
		}
	}

	auto_accept = yes

	on_accept = {
		scope:actor = {
			divorce_effect = {
				DIVORCER = scope:actor
				DIVORCEE = scope:recipient
			}
		}
		scope:recipient = {
			trigger_event = marriage_interaction.0030
		}
	}
	
	# AI
	ai_targets = {
		ai_recipients = spouses
	}
	ai_frequency = 36
	
	ai_potential = {
		is_ruler = yes
	}

	ai_will_do = {
		base = 0

		modifier = { # The AI wants to divorce should they not have an heir that is their own child, and their spouse is infertile
			add = 100
			scope:actor = {
				fertility > 0.1
				NOT = {
					any_child = {
						is_heir_of = scope:actor
					}
				}
			}
			scope:recipient = {
				is_ai = yes
				fertility < 0.1
			}
		}

		modifier = { # The AI will divorce if they're in a same-sex marriage and their faith changes its stance on the topic
			add = 100
			scope:actor = {
				sex_same_as = scope:recipient
				allowed_to_marry_same_sex_trigger = no
			}
		}

		# Unity modifiers
		evaluate_action_decreasing_house_unity = {
			VALUE = 100
		}
	}
}

#Demote a spouse to a concubine
demote_spouse_interaction = {
	category = interaction_category_diplomacy
	use_diplomatic_range = no
	icon = concubine_icon

	desc = demote_spouse_interaction_desc
	cost = { 
		piety = divorce_cost 
		influence = {
			value = 0
			if = {
				limit = { 
					scope:actor = {
						government_has_flag = government_has_influence 
					}
				}
				if = { #If recipient comes from a powerful family or the same rank, some influence
					limit = {
						scope:recipient = {
							any_close_family_member = {
								NOR = { 
									this = scope:actor 
									is_child_of = scope:actor
								}
								OR = {
									is_powerful_vassal_of = scope:actor
									is_councillor_of = scope:actor
									tier_difference = {
										target = scope:actor
										value = 0
									}
									AND = { #Or belongs to the same realm
										government_has_flag = government_has_influence
										exists = scope:actor.top_liege
										exists = scope:actor.house
										top_liege ?= {
											this = scope:actor.top_liege
										}
										house ?= {
											is_powerful_family = yes
											NOT = { this = scope:actor.house }
											is_dominant_family = no
										}
									}
								}
							}
						}
					}
					add = medium_influence_value
				}
				if = { #If recipient comes from a dominant family, massive influence
					limit = {
						scope:recipient = {
							any_close_family_member = {
								NOR = { 
									this = scope:actor 
									is_child_of = scope:actor
								}
								OR = {
									tier_difference = {
										target = scope:actor
										value > 0
									}
									AND = { #Or belongs to the same realm
										government_has_flag = government_has_influence
										OR = {
											AND = {
												exists = scope:actor.top_liege
												top_liege ?= {
													this = scope:actor.top_liege
												}
											}
											is_vassal_or_below_of = scope:actor
										}
										exists = scope:actor.house
										house ?= {
											NOT = { this = scope:actor.house }
											is_dominant_family = yes
										}
									}
								}
							}
						}
					}
					add = {
						value = major_influence_value
						if = {
							limit = { #More for someone who far outranks you
								scope:recipient = {
									any_close_family_member = {
										NOR = { 
											this = scope:actor 
											is_child_of = scope:actor
										}
										tier_difference = {
											target = scope:actor
											value > 1
										}
									}
								}
							}
							add = major_influence_value
						}
					}
				}
			}
		}
		prestige = {
			value = 0
			if = {
				limit = { 
					NOT = { 
						scope:actor = {
							government_has_flag = government_has_influence 
						}
					} 
				}
				if = { #If recipient comes from a powerful family or the same rank, some prestige
					limit = {
						scope:recipient = {							
							any_close_family_member = {
								NOR = { 
									this = scope:actor 
									is_child_of = scope:actor
								}
								OR = {
									is_powerful_vassal_of = scope:actor
									is_councillor_of = scope:actor
									tier_difference = {
										target = scope:actor
										value = 0
									}
									AND = { #Or belongs to the same realm
										government_has_flag = government_has_influence
										exists = scope:actor.top_liege
										exists = scope:actor.house
										top_liege ?= {
											this = scope:actor.top_liege
										}
										house ?= {
											is_powerful_family = yes
											NOT = { this = scope:actor.house }
											is_dominant_family = no
										}
									}
								}
							}
						}
					}
					add = medium_prestige_value
				}
				if = { #If recipient comes from a dominant family, massive prestige
					limit = {
						scope:recipient = {
							any_close_family_member = {
								NOR = { 
									this = scope:actor 
									is_child_of = scope:actor
								}
								OR = {
									tier_difference = {
										target = scope:actor
										value > 0
									}
									AND = { #Or belongs to the same realm
										government_has_flag = government_has_influence
										OR = {
											AND = {
												exists = scope:actor.top_liege
												top_liege ?= {
													this = scope:actor.top_liege
												}
											}
											is_vassal_or_below_of = scope:actor
										}
										exists = scope:actor.house
										house ?= {
											NOT = { this = scope:actor.house }
											is_dominant_family = yes
										}
									}
								}
							}
						}
					}
					add = {
						value = major_prestige_value
						if = {
							limit = { #More for someone who far outranks you
								scope:recipient = {
									any_close_family_member = {
										NOR = { 
											this = scope:actor 
											is_child_of = scope:actor
										}
										tier_difference = {
											target = scope:actor
											value > 1
										}
									}
								}
							}
							add = major_prestige_value
						}
					}
				}
			}
		}
	}

	is_shown = {
		scope:actor = {
			faith = {
				# We are not required to seek approval of a spiritual HoF
				NAND = {
					has_doctrine = doctrine_divorce_approval
					has_doctrine = doctrine_spiritual_head
					exists = religious_head
				}
				# We are not required to seek approval of a house head
				NAND = {
					has_doctrine = doctrine_divorce_approval
					exists = scope:actor.house
					exists = scope:actor.house.house_head
					scope:actor = { is_house_head = no }
				}
			}
			hp_accepts_concubinage = yes
		}
		scope:recipient = {
			is_spouse_of = scope:actor
			is_concubine = no # Can set aside Concubines at any time
			is_landed = no
			is_imprisoned = no
		}
	}

	is_valid_showing_failures_only = {
		custom_description = {
			text = take_concubine_allowed_more
			object = scope:recipient
			scope:actor = {
				allowed_more_concubines = yes
			}
		}
		custom_tooltip = {
			NOT = {
				scope:actor.culture = {
					has_cultural_parameter = cannot_divorce
				}
			}
			text = CULTURE_FORBIDS_DIVORCE
		}
		custom_tooltip = {
			scope:actor = { NOT = { religion = religion:islam_religion } }
			text = take_concubine_ex_spouse_faith
		}
		# custom_tooltip = {
		# 	scope:recipient = {
		# 		NOT = { has_character_flag = flag_new_consort }
		# 	}
		# 	text = RECENTLY_MARRIED_SPOUSE
		# }
		scope:recipient = { is_busy_in_events_localised = yes }
		scope:recipient = { NOT = { has_strong_hook = scope:actor } }
		scope:actor = { NOT = { allowed_more_concubines = no } }
	}

	auto_accept = yes

	on_accept = {
		scope:actor = {
			save_scope_as = secondary_actor
			show_as_tooltip = {
				make_concubine = scope:recipient
			}
		}
		scope:recipient = {
			save_scope_as = secondary_recipient
		}
		spouse_demotion_effect = {
			ACTOR = scope:actor
			RECIPIENT = scope:recipient
		}
	}
	
	# AI
	ai_targets = {
		ai_recipients = spouses
	}
	ai_frequency = 36
	
	ai_potential = {
		is_ruler = yes
	}

	ai_will_do = {
		base = 0

		modifier = { #More likely if criminal
			add = 10
			scope:actor = {
				has_trait = just
				has_imprisonment_reason = scope:recipient
			}
		}
		modifier = { #Secret
			scope:actor = {
				OR = {
					has_trait = just
					has_trait = honest
				}
			}
			scope:recipient = {
				any_secret = {
					is_known_by = scope:actor
					is_criminal_for = secret_owner
				}
			}
			add = 10
		}
		modifier = { #Rival
			scope:actor = {
				has_relation_rival = scope:recipient
				has_relation_nemesis = scope:recipient
			}
			add = 20
		}
		modifier = { #Influential consort
			scope:recipient = {
				any_close_family_member = {
					OR = {
						is_powerful_vassal_of = scope:actor
						is_councillor_of = scope:actor
						highest_held_title_tier >= tier_kingdom
						trigger_if = {
							limit = {
								scope:actor = {
									government_has_flag = government_is_administrative
									exists = house
								}
							}
							government_has_flag = government_is_administrative
							house ?= {
								is_dominant_family = yes
								NOT = { this = scope:actor.house }
							}
							trigger_if = {
								limit = {
									exists = scope:actor.top_liege
								}
								top_liege ?= {
									this = scope:actor.top_liege
								}
							}
							trigger_else = {
								top_liege ?= {
									this = scope:actor
								}
							}
						}
					}
				}
			}
			add = -50
		}
		modifier = { #Influential consort
			scope:recipient = {
				any_close_family_member = {
					tier_difference = {
						target = scope:actor
						value >= -1
					}
				}
			}
			add = -15
		}
		modifier = { #Influential consort
			scope:recipient = {
				any_close_family_member = {
					tier_difference = {
						target = scope:actor
						value >= 0
					}
				}
			}
			add = -10
		}
		modifier = { #Influential consort
			scope:recipient = {
				any_close_family_member = {
					tier_difference = {
						target = scope:actor
						value >= 1
					}
				}
			}
			add = -10
		}
		modifier = { #Influential consort
			scope:recipient = {
				any_close_family_member = {
					is_liege_or_above_of = scope:actor
				}
			}
			add = -150
		}
		modifier = { #Sinner
			scope:recipient = {
				num_sinful_traits >= 1
				num_virtuous_traits = 0
			}
			scope:actor = {
				has_trait = zealous
			}
			add = 10
		}
		modifier = { #Unfavored
			scope:recipient = {
				has_character_modifier = unfavored_consort_modifier
			}
			add = 10
		}
		modifier = { #Favored
			scope:recipient = {
				has_character_modifier = favored_consort_modifier
			}
			add = -20
		}

		# Unity modifiers
		evaluate_action_decreasing_house_unity = {
			VALUE = 100
		}
	}
}

offer_concubine = {
	category = interaction_category_diplomacy
	icon = concubine_icon

	desc = offer_concubine_desc

	ai_targets = {
		ai_recipients = neighboring_rulers
		ai_recipients = suzerain
		ai_recipients = liege
		ai_recipients = family
		max = 5
	}
	ai_target_quick_trigger = {
		adult = yes
	}
	ai_frequency = 36

	greeting = positive
	notification_text = OFFER_CONCUBINE_NOTIFICATION

	#interface = concubine_list

	needs_recipient_to_open = yes

	populate_actor_list = {
		scope:actor = {
			every_courtier = {
				limit = {
					can_be_offered_as_concubine_to_character_trigger = {
						GIVER = scope:actor
						CHARACTER = scope:recipient
					}
				}
				add_to_list = characters
			}
			every_prisoner = {
				limit = {
					NOT = { is_in_list = characters }
					can_be_offered_as_concubine_to_character_trigger = {
						GIVER = scope:actor
						CHARACTER = scope:recipient
					}
				}
				add_to_list = characters
			}
		}
	}

	is_shown = {
		NOT = { scope:actor = scope:recipient }
		scope:actor = {
			hp_accepts_concubinage = yes
		}
		scope:recipient = {
			NOT = { government_has_flag = government_is_herder }
			allowed_concubines = yes
			is_adult = yes
		}
	}

	is_valid_showing_failures_only = {
		custom_description = {
			text = take_concubine_available_character_opp
			object = scope:recipient
			scope:actor = {
				OR = {
					any_courtier = {
						can_be_offered_as_concubine_to_character_trigger = {
							GIVER = scope:actor
							CHARACTER = scope:recipient
						}
					}
					any_prisoner = {
						can_be_offered_as_concubine_to_character_trigger = {
							GIVER = scope:actor
							CHARACTER = scope:recipient
						}
					}
				}
			}
		}
		custom_description = {
			text = take_concubine_allowed_more
			object = scope:recipient
			scope:recipient = {
				allowed_more_concubines = yes
			}
		}
	}

	can_be_picked = {
		is_adult = yes
	}

	can_send = {
		custom_description = {
			text = character_unavailable_to_be_offered_as_concubine
			object = scope:secondary_actor
			scope:secondary_actor = {
				can_be_offered_as_concubine_to_character_trigger = {
					GIVER = scope:actor
					CHARACTER = scope:recipient
				}
			}
		}
	}

	auto_accept = no

	ai_accept = {
		base = 50
		
		modifier = {
			add = {
				value = 50
				multiply = scope:secondary_actor.num_of_good_genetic_traits
			}
			scope:secondary_actor = {
				num_of_good_genetic_traits > 0
				trigger_if = {
					limit = {
						is_female = yes
					}
					is_aging_character = no
				}
			}
			desc = AI_POSITIVE_TRAIT_REASON
		}

		modifier = {
			add = {
				value = 50
			}
			scope:secondary_actor = {
				is_lowborn = no
				trigger_if = {
					limit = {
						is_female = yes
					}
					is_aging_character = no
				}
			}
			desc = AI_DYNASTY_PRESTIGE_REASON_NOBLE
		}

		modifier = {
			add = {
				value = -50
				multiply = scope:secondary_actor.num_of_bad_genetic_traits
			}
			scope:secondary_actor = {
				OR = {
					num_of_bad_genetic_traits > 0
				}
				trigger_if = {
					limit = {
						is_female = yes
					}
					is_aging_character = no
				}
			}
			desc = AI_NEGATIVE_TRAIT_REASON
		}

		modifier = {
			add = {
				value = -50
			}
			scope:secondary_actor = {
				is_lowborn = yes
				num_of_good_genetic_traits = 0
				trigger_if = {
					limit = {
						is_female = yes
					}
					is_aging_character = no
				}
			}
			desc = AI_DYNASTY_PRESTIGE_REASON_LOWBORN_NO_POSITIVE_TRAIT
		}

		opinion_modifier = { # Opinion of you
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 1.0
			desc = AI_OPINION_REASON
		}

		opinion_modifier = { # Opinion of concubine
			who = scope:recipient
			opinion_target = scope:secondary_actor
			multiplier = 0.25
			desc = AI_OPINION_REASON
		}

		compare_modifier = { # Recipient is reluctant to marry old women (procreation is a key factor)
			trigger = {
				scope:secondary_actor = {
					is_young_character = no
					is_female = yes
				}
			}
			target = scope:secondary_actor
			value = age
			multiplier = -5.0
			step = 1
			offset = -29
			desc = MARRY_AGE
		}

		modifier = { # A Recipient is unwilling to take a same-sex concubine if their faith doesn't support it
			add = -1000
			
			scope:recipient = {
				allowed_to_marry_same_sex_trigger = no
				sex_same_as = scope:secondary_actor
			}
			desc = SAME_SEX_MARRIAGE_FAITH_REASON_RECIPIENT
		}

		modifier = { # Unwilling if they don't have a batch of kids and opposite sex consorts to handle that situation
			add = -200
			scope:recipient = {
				allowed_to_marry_same_sex_trigger = yes
				sex_same_as = scope:secondary_actor
				trigger_if = {
					limit = {
						NOT = {
							any_consort = {
								can_have_children_with = { CHARACTER = scope:recipient }
								fertility > 0.1
							}
						}
					}
					any_child = { is_player_heir_of = root }
				}
				trigger_else = {
					always = no
				}
			}
			desc = AI_NO_FERTILITY_REASON
		}

		modifier = {
			add = 50
			scope:recipient = {
				is_attracted_to_gender_of = scope:secondary_actor
			}
			desc = AI_ATTRACTION_REASON
		}

		modifier = { # A Recipient is more likely to agree if you're their liege
			add = 40
			
			scope:recipient = {
				target_is_liege_or_above = scope:actor
			}
			desc = LIEGE_REASON
		}

		#More/Less likely based on difference in rank between actor and recipient
		modifier = {
			add = 20
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 1
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = 30
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 2
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = 40
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 3
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = 50
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 4
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = 60
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = 5
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = -20
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -1
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = -30
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -2
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = -40
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -3
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = -50
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -4
				}
			}
			desc = AI_RANK_DIFF
		}
		modifier = {
			add = -60
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value = -5
				}
			}
			desc = AI_RANK_DIFF
		}
		
		modifier = {
			add = intimidated_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		
		modifier = {
			add = intimidated_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		modifier = {
			add = -250
			scope:secondary_actor = {
				has_std_trigger = yes
			}
			desc = AI_STD_REASON
		}
		modifier = {
			add = -250
			scope:secondary_actor = {
				has_epidemic_disease_trigger = yes
			}
			desc = AI_CONTAGIOUS_REASON
		}
		modifier = {
			add = -5000
			scope:secondary_actor = {
				fertility <= 0
			}
			desc = AI_FERTILITY_REASON
		}
		modifier = {
			scope:hook = yes
			add = 500
			scope:actor = { NOT = { has_strong_hook = scope:recipient } }
			desc = SCHEME_WEAK_HOOK_USED
		}
		modifier = {
			scope:hook = yes
			add = 1000
			scope:actor = { has_strong_hook = scope:recipient }
			desc = SCHEME_STRONG_HOOK_USED
		}

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}

		# Harem Politics modifiers
		modifier = {
			scope:recipient = {
				has_trait = paranoid
			}
			add = -200
			desc = AI_PARANOID_TRAIT_REASON
		}
		modifier = {
			scope:recipient = {
				has_trait = chaste
			}
			add = {
				value = -100
				if = {
					limit = {
						scope:recipient = {
							number_of_concubines < number_of_desired_concubines
						}
					}
					add = 15
				}
			}
			desc = AI_CHASTE_TRAIT_REASON
		}
		modifier = {
			scope:recipient = {
				has_trait = celibate
			}
			add = {
				value = -150
				if = {
					limit = {
						scope:recipient = {
							number_of_concubines < number_of_desired_concubines
						}
					}
					add = 25
				}
			}
			desc = AI_CELIBATE_TRAIT_REASON
		}

		modifier = {
			scope:actor = {
				government_has_flag = government_has_influence
				exists = top_liege
			}
			scope:recipient = {
				exists = house
				government_has_flag = government_has_influence
				OR = {
					this = scope:actor.top_liege
					top_liege ?= scope:actor.top_liege
				}
				house ?= {
					OR = {
						is_powerful_family = yes
						is_dominant_family = yes
					}
				}
			}
			scope:secondary_actor.house ?= scope:actor.house
			add = {
				value = -110
				if = {
					limit = {
						scope:actor = {
							exists = house
							exists = top_liege
							government_has_flag = government_has_influence
						}
						scope:recipient = {
							exists = house
							house != scope:actor.house
							government_has_flag = government_has_influence
							OR = { # Imperial Family check
								this = scope:actor.top_liege # Recipient is actor's top liege
								AND = {
									top_liege ?= scope:actor.top_liege # Same top liege
									exists = scope:actor.top_liege.house
									house ?= scope:actor.top_liege.house # Recipient is member of top liege's house
								}
							}
						}
					}
					add = -30 # Strong penalty for Imperial Family
				}
				if = {
					limit = {
						scope:actor = {
							government_has_flag = government_has_influence
							exists = house
							exists = top_liege
						}
						scope:recipient = {
							government_has_flag = government_has_influence
							exists = house
							top_liege ?= scope:actor.top_liege # Same realm check
							house = {
								is_dominant_family = yes
								NOT = { this = scope:actor.house }
							}
						}
					}
					add = -15 # Penalty for dominant family
				}
				if = {
					limit = {
						scope:recipient = {
							has_trait = ambitious
						}
					}
					add = -15
				}
				if = {
					limit = {
						scope:recipient = {
							number_of_concubines < number_of_desired_concubines
						}
					}
					add = 15
				}
			}
			desc = AI_FAMILY_STATUS_REASON
		}
		modifier = {
			add = 10
			scope:actor = {
				government_has_flag = government_has_influence
				exists = house
				exists = top_liege
				house = {
					is_powerful_family = yes
				}
			}
			scope:recipient = {
				government_has_flag = government_has_influence
				exists = house
				top_liege ?= scope:actor.top_liege # Same realm check
			}
			scope:secondary_actor.house ?= scope:actor.house
			desc = ACTOR_POWERFUL_HOUSE_REASON
		}
		modifier = {
			add = 20
			scope:actor = {
				government_has_flag = government_has_influence
				exists = house
				exists = top_liege
				house = {
					is_dominant_family = yes
				}
			}
			scope:recipient = {
				government_has_flag = government_has_influence
				exists = house
				top_liege ?= scope:actor.top_liege # Same realm check
			}
			scope:secondary_actor.house ?= scope:actor.house
			desc = ACTOR_DOMINANT_HOUSE_REASON
		}
	}

	ai_potential = {
		is_adult = yes
		NOT = { has_trait = incapable }
		is_at_war = no
		NOR = {
			has_trait = greedy
			ai_greed > very_high_positive_ai_value
		}	
		hp_accepts_concubinage = yes
	}

	ai_will_do = {
		base = 0

		modifier = { # Basic Filtering
			add = 100
			scope:recipient = {
				number_of_concubines < number_of_desired_concubines
			}
			scope:secondary_actor = {
				trigger_if = {
					limit = {
						allowed_to_marry_same_sex_trigger = no
					}
					sex_opposite_of = scope:recipient
				}
				fertility > 0.2
				is_young_character = yes
				NOR = {
					is_councillor_of = scope:actor
					is_of_major_interest_trigger = {
						CHARACTER = scope:actor
					}
					is_concubine_of = scope:actor
				}
			}
		}

		modifier = { # If secondary_actor is close to actor, a good character, or if actor should take them as concubine
			add = -120
			scope:secondary_actor = {
				is_of_minor_interest_trigger = {
					CHARACTER = scope:actor
				}
				AND = {
					can_become_concubine_of_character_trigger = { CHARACTER = scope:actor }
					scope:actor = { number_of_concubines < number_of_desired_concubines }
				}
			}
		}

		modifier = { # If secondary_actor could be an agent...
			add = 25
			scope:actor = {
				any_scheme = {
					scheme_target_character = scope:recipient
					save_temporary_scope_as = scheme_check
				}
			}
			scope:secondary_actor = {
				char_can_fit_into_scheme_trigger = { SCHEME = scope:scheme_check }
			}
		}
		
		modifier = { # More likely if it's a neighboring ruler that you want to like you
			add = 50
			scope:recipient = {
				any_neighboring_top_liege_realm_owner = { this = scope:actor }
				max_military_strength > scope:actor.max_military_strength
			}
		}
		
		modifier = { # More likely if it's your friend and it's a nice concubine
			add = 50
			scope:recipient = {
				OR = {
					has_relation_friend = scope:actor
					has_relation_best_friend = scope:actor
				}
			}
			scope:secondary_actor = {
				fertility > 0.2
				is_young_character = yes
				OR = {
					has_trait = lustful
					num_of_good_genetic_traits > 0
				}
			}
		}

		modifier = { #Even if they're allowed to take same-sex concubines we don't want the AI to suggest it without a good reason if they're not attracted to the person
			add = -200
			scope:recipient = {
				allowed_to_marry_same_sex_trigger = yes
				sex_same_as = scope:secondary_actor
				NOT = { is_attracted_to_gender_of = scope:secondary_actor }
			}
		}

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}

		# Harem Politics modifiers
		modifier = { # More likely if both are administrative and recipient's house is important
			add = {
				value = 20
				if = {
					limit = {
						scope:actor = {
							has_trait = ambitious
						}
					}
					add = 15
				}
				if = {
					limit = {
						scope:actor = {
							has_trait = content
						}
					}
					add = -15
				}
				if = {
					limit = {
						scope:actor = {
							has_trait = humble
						}
					}
					add = -15
				}
				if = {
					limit = {
						scope:actor = {
							has_character_flag = received_influence_offer_concubine_flag
						}
					}
					multiply = 0
				}
			}
			scope:actor = {
				government_has_flag = government_has_influence
				exists = house
				exists = top_liege
			}
			scope:recipient = {
				government_has_flag = government_has_influence
				top_liege ?= scope:actor.top_liege
				house ?= {
					OR = {
						is_powerful_family = yes
						is_dominant_family = yes
					}
					NOR = {
						this = scope:actor.house
						this = scope:actor.top_liege.house
					}
				}
			}
			scope:secondary_actor.house ?= scope:actor.house
		}
		modifier = { # More likely if both are administrative and recipient's house is important
			add = {
				value = 30
				if = {
					limit = {
						scope:actor = {
							has_trait = ambitious
						}
					}
					add = 15
				}
				if = {
					limit = {
						scope:actor = {
							has_trait = content
						}
					}
					add = -15
				}
				if = {
					limit = {
						scope:actor = {
							has_trait = humble
						}
					}
					add = -15
				}
				if = {
					limit = {
						scope:actor = {
							has_character_flag = received_influence_offer_concubine_flag
						}
					}
					multiply = 0
				}
			}
			scope:actor = {
				government_has_flag = government_has_influence
				exists = house
				exists = top_liege
			}
			scope:recipient = {
				government_has_flag = government_has_influence
				house ?= {
					NOT = {
						this = scope:actor.house
					}
				}
				OR = { # Same realm check
					this = scope:actor.top_liege
					AND = {
						top_liege ?= scope:actor.top_liege
						house ?= {
							any_house_member = {
								this = scope:actor.top_liege
							}
						}
					}
				}
			}
			scope:secondary_actor.house ?= scope:actor.house
		}

		modifier = { #More likely if the recipient is a child
			scope:recipient = {
				is_child_of = scope:actor
				reverse_opinion = {
					target = scope:actor
					value > -11
				}
				number_of_concubines < number_of_desired_concubines
			}
			add = {
				value = 20
				if = {
					limit = {
						scope:recipient = {
							is_primary_heir_of = scope:actor
						}
					}
					add = 10
				}
				if = {
					limit = {
						scope:actor.primary_title = {
							place_in_line_of_succession = {
								target = scope:recipient
								value <= 3
							}
						}
					}
					add = 5
				}
				if = {
					limit = {
						scope:recipient = {
							any_child = {
								is_alive = yes
								count < 2
							}
						}
					}
					add = 5
				}
			}
		}
		modifier = { #More likely if the recipient is a relative
			scope:recipient = {
				is_close_family_of = scope:actor
				reverse_opinion = {
					target = scope:actor
					value > -6
				}
				number_of_concubines < number_of_desired_concubines
				NOT = { is_child_of = scope:actor }
			}
			add = {
				value = 5
				if = {
					limit = {
						scope:recipient = {
							is_primary_heir_of = scope:actor
						}
					}
					add = 10
				}
			}
		}

		modifier = { # No if it's a same-sex concubinage and actor isn't accepting of it
			factor = 0
			scope:actor = {
				allowed_to_marry_same_sex_trigger = no
				scope:recipient = { sex_same_as = scope:secondary_actor }
			}
		}

		modifier = { # No if it's a same-sex concubinage and recipient isn't accepting of it
			factor = 0
			scope:recipient = {
				allowed_to_marry_same_sex_trigger = no
				sex_same_as = scope:secondary_actor
			}
		}

		modifier = { # Age considerations
			factor = 0
			scope:recipient = {
				OR = {
					age >= scope:secondary_actor.age_plus_10
					age <= scope:secondary_actor.age_minus_10
					scope:secondary_actor.age >= 30 # Concubines are meant to be young...
				}
			}
		}

		modifier = { # Incest Filtering: Hard NO!
			factor = 0
			scope:recipient = {
				relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:secondary_actor }
				NOR = {
					accepts_incest_with_trigger = { CHARACTER = scope:secondary_actor }
					sexually_liberal_trigger = yes
				}
			}
		}

		modifier = { # Incest Filtering: Not likely, but keep the door open...
			factor = 0.2 
			scope:recipient = {
				relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:secondary_actor }
				OR = {
					accepts_incest_with_trigger = { CHARACTER = scope:secondary_actor }
					sexually_liberal_trigger = yes
				}
			}
		}

		modifier = { # Faith considerations
			factor = 0
			scope:recipient = {
				faith = {
					faith_hostility_level = {
						target = scope:actor.faith
						value >= faith_hostile_level
					}
				}
				NOR = {
					has_relation_friend = scope:actor
					has_relation_best_friend = scope:actor
				}
			}
		}
	}

	on_send = {
		scope:secondary_actor = { # to block the same character from being offered twice
			add_character_flag = {
				flag = has_been_offered_as_concubine
				days = 5
			}
		}
	}
	
	on_accept = {
		concubine_offer_on_accept_effect = yes
	}

	on_decline = {
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_bad
				title = msg_concubine_offer_rejected_title
				right_icon = scope:recipient
				left_icon = scope:secondary_actor
				custom_tooltip = msg_concubine_offer_rejected
			}
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_loss
			DESC = clan_unity_concubinage_offer_decline.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

	send_options_exclusive = no
	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = SCHEME_HOOK
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"
}